package homework_4_minesweeper;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class Cell extends JButton {
    private static final long serialVersionUID = 1L;
    private static final Color UNREVEALED = new Color(192, 192, 192);
    private static final Color REVEALED = Color.GRAY;
    private static final Font FONT_NUMBERS = new Font("Monospaced", Font.BOLD, 19);
    private int row, col;
    private boolean isRevealed;
    private boolean hasMine;
    private boolean isFlagged;
    private int num;
    private int numberOfMinesAround;
    public Cell(){
        this.row=0;
        this.col=0;
        super.setFont(FONT_NUMBERS);
    }
    public Cell(int row, int col) {
        setIcon(null);
        setText("");
        this.row = row;
        this.col = col;
        super.setFont(FONT_NUMBERS);
        this.num=MinesweeperUtilities.codeToNum(this.row,this.col,GameBoardPanel.getCol());
        this.hasMine=false;
        this.isFlagged=false;
        this.setBackground(UNREVEALED);
        this.setPreferredSize(new Dimension(45,45));
    }

    public boolean getRevealedStatus(){
        return isRevealed;
    }

    public boolean isFlagged() {
        return isFlagged;
    }

    public void setNumberOfMinesAround(int numberOfMinesAround) {
        this.numberOfMinesAround = numberOfMinesAround;
    }

    public int getNumberOfMinesAround() {
        return numberOfMinesAround;
    }

    public int getPosition() {
        return MinesweeperUtilities.codeToNum(row,col,GameBoardPanel.getCol());
    }

    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }

    public void flag(){
        if (isFlagged || isRevealed) {
            isFlagged = false;
            setIcon(null);
            if(!isRevealed) {
                setText("");
                SouthMenu.increaseMine();
            }
        } else {
            isFlagged = true;
            SouthMenu.decreaseMine();
            try {
                BufferedImage flag= ImageIO.read(Cell.class.getResource("images/flag.png"));
                setIcon(new ImageIcon(flag));
            } catch (Exception e) {
                setForeground(Color.RED);
                setText("🚩");
            }
        }
    }
    public void reveal(){
        if(isFlagged){
            SouthMenu.increaseMine();
        }
        this.isRevealed = true;
        this.isFlagged = false;
        setIcon(null);
        GameBoardPanel.decrease();
        paint();
        //后面还要添加数字显示等等。
    }
    public void reset(){
        this.isFlagged=false;
        this.isRevealed=false;
        setIcon(null);
        setText("");
        paint();
    }

    public void paint() {
        super.setBackground(isRevealed? REVEALED:UNREVEALED);
    }

    public void setColor(int numOfMines) {
        switch (numOfMines){
            case 1:{
                setForeground(Color.BLUE);
                break;
            }
            case 2:{
                setForeground(new Color(0,128,0));
                break;
            }
            case 3:{
                setForeground(Color.RED);
                break;
            }
            case 4:{
                setForeground(new Color(0,0,128));
                break;
            }
            case 5:{
                setForeground(new Color(128,0,0));
                break;
            }
            case 6:{
                setForeground(new Color(0,128,128));
                break;
            }
            case 7:{
                setForeground(Color.BLACK);
                break;
            }
            case 8:{
                setForeground(Color.MAGENTA);
                break;
            }
            default:{
                setForeground(Color.BLACK);
                break;
            }
        }
    }

    public void setIconMine() {
        try {
            BufferedImage flag= ImageIO.read(Cell.class.getResource("images/mine.png"));
            setIcon(new ImageIcon(flag));
        } catch (Exception e) {
            setForeground(Color.BLACK);
            setText("太阳");
        }
    }
}
